package com.widget.wallpaper;


import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

public class WNWService extends Service{
	public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
	public static String ACTION_WIDGET_CHANGE_UI = "ChangeUI";
	public static String CHARACTER_WALLPAPER = "penguin";
	private String option_status= "hide"; //set default value to string
	public static String config_wallpaper_char = "penguin";
	@Override
	public void onStart(Intent intent, int startId) {
		//check show-hide previous state
		Bundle extras = intent.getExtras();
		if (extras != null) {
//			Log.d("character","before start real"+extras.getString("OPTION_DISPLAY_STATUS"));
//			Log.d("character","before start variable"+option_status);

			if(extras.getString("OPTION_DISPLAY_STATUS")!=null)
				option_status = extras.getString("OPTION_DISPLAY_STATUS");
			if(extras.getString("INTENT_SHOW_INPUT")!=null)
				if(extras.getString("INTENT_SHOW_INPUT").equalsIgnoreCase("TRUE"))
					option_status ="hide"; //treat it likes hiding; it will show in buildUpdate
				else
					option_status ="show";
			if(extras.getString("CHARACTER_WALLPAPER")!=null)
			{
				option_status ="hide"; //treat it likes hiding; it will show in buildUpdate
				config_wallpaper_char = extras.getString("CHARACTER_WALLPAPER");
			}

			//&& extras.getString("INTENT_FROM_INPUT_ACTIVITY").equalsIgnoreCase("TRUE")
				//option_status = extras.getString("OPTION_DISPLAY_STATUS");
			//Log.d("character","after start real"+extras.getString("INTENT_FROM_INPUT_ACTIVITY"));
//			Log.d("character","after start variable"+option_status);Intent_FROM_INPUT_ACTIVITY
		}

		// Build the widget update.
		RemoteViews updateViews = buildUpdate(this);
//		Log.d("character", "updateViews");
		// Push update for this widget to the home screen
		ComponentName WNWidget = new ComponentName(this,
				WNWProvider.class);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
		appWidgetManager.updateAppWidget(WNWidget, updateViews);
	}
	private RemoteViews buildUpdate(Context context) {
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.main);

		
		views.setTextViewText(R.id.messageTxt, "Click textbox to edit\n wallpaper text");
		//get and set character on the widget
		String character = WNWConfig.getCharacterPref(context);
		Resources res = getResources();
		String mDrawableName = "@drawable/"+character+"1";
		int resID = res.getIdentifier(mDrawableName, "drawable", context.getPackageName());
		views.setImageViewResource(R.id.imageView1, resID);
//		Log.d("character",mDrawableName);
		
		String backgroundStr = inputActivity.getBackgroundPref(context);
//		Log.d("character","abc"+backgroundStr);
		
		views.setTextViewText(R.id.textView1, backgroundStr);
		
		//change character
		Intent iChar = new Intent(this, WNWConfig.class);
		iChar.setAction("optionStatusHandlerService");
		//put current state of input to intent;
		if(option_status.equalsIgnoreCase("hide")) 
		{
			iChar.putExtra("INTENT_SHOW_INPUT", "TRUE");
		}
		else
			iChar.putExtra("INTENT_SHOW_INPUT", "FALSE");
		PendingIntent pendingChar = PendingIntent.getActivity(context,0, iChar,PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.imageView1,pendingChar);
		Log.d("character","char_wall:"+config_wallpaper_char);
		
		
		//set character preference for wallpaper
		Intent iCharacter1 = new Intent(this, WNWService.class);
		iCharacter1.setAction("wallpaperCharacterDummie");
		iCharacter1.removeExtra("CHARACTER_WALLPAPER");
		iCharacter1.putExtra("CHARACTER_WALLPAPER", "penguin");
		PendingIntent pendingCharacter1 = PendingIntent.getService(context,0, iCharacter1,PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.layout_char_1,pendingCharacter1);
//		Log.d("character","end_penguin1:"+iCharacter1.getExtras().get("CHARACTER_WALLPAPER"));
		//Log.d("character","end_penguin3:"+iCharacter3.getExtras().get("CHARACTER_WALLPAPER"));
//		Intent iCharacter2 = new Intent(this, WNWService.class);
//		iCharacter2.setAction("wallpaperCharacterDummie");
//		iCharacter2.removeExtra("CHARACTER_WALLPAPER");
		iCharacter1.putExtra("CHARACTER_WALLPAPER", "black");
		PendingIntent pendingCharacter2 = PendingIntent.getService(context,4, iCharacter1,PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.layout_char_2,pendingCharacter2);
		
//		Intent iCharacter3 = new Intent(this, WNWService.class);
//		iCharacter3.setAction("wallpaperCharacterDummie");
//		iCharacter3.removeExtra("CHARACTER_WALLPAPER");
		iCharacter1.putExtra("CHARACTER_WALLPAPER", "white");
		PendingIntent pendingCharacter3 = PendingIntent.getService(context,5, iCharacter1,PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.layout_char_3,pendingCharacter3);
		
		//initialize selected character preferenc for wallpaper
				if(config_wallpaper_char.equalsIgnoreCase("penguin"))
				{
					views.setViewVisibility(R.id.circle_1, View.VISIBLE);
				views.setViewVisibility(R.id.circle_2, View.INVISIBLE);
					views.setViewVisibility(R.id.circle_3, View.INVISIBLE);
					Log.d("character","choose penguin:"+config_wallpaper_char);
					
				}
				else if(config_wallpaper_char.equalsIgnoreCase("black"))
				{
					views.setViewVisibility(R.id.circle_2, View.VISIBLE);
					views.setViewVisibility(R.id.circle_1, View.INVISIBLE);
					views.setViewVisibility(R.id.circle_3, View.INVISIBLE);
					
				}
				else if(config_wallpaper_char.equalsIgnoreCase("white"))
				{
					views.setViewVisibility(R.id.circle_3, View.VISIBLE);
					views.setViewVisibility(R.id.circle_1, View.INVISIBLE);
					views.setViewVisibility(R.id.circle_2, View.INVISIBLE);
					
				}
				else
				{
					views.setViewVisibility(R.id.circle_1, View.INVISIBLE);
					views.setViewVisibility(R.id.circle_2, View.INVISIBLE);
					views.setViewVisibility(R.id.circle_3, View.INVISIBLE);
					
				}
		
		
//		Log.d("character","end_white1:"+iCharacter1.getExtras().get("CHARACTER_WALLPAPER"));
//		Log.d("character","end_white3:"+iCharacter3.getExtras().get("CHARACTER_WALLPAPER"));
		//change wallpaper
		Intent iWallpaper = new Intent(this, WallpaperService.class);
		PendingIntent pendingWallpaper = PendingIntent.getService(context,6, iWallpaper,0);
		views.setOnClickPendingIntent(R.id.button1,pendingWallpaper);
		
		//change input text
		Intent iText = new Intent(this, inputActivity.class);
		PendingIntent pendingText = PendingIntent.getActivity(context,2, iText,0);
		views.setOnClickPendingIntent(R.id.textView1,pendingText);
		
		
		//show-hide option
			Intent iCloud = new Intent(this, WNWService.class);
			iCloud.setAction("optionStatusHandlerService");
			if(option_status.equalsIgnoreCase("hide"))
			{
				iCloud.removeExtra("OPTION_DISPLAY_STATUS");
				iCloud.putExtra("OPTION_DISPLAY_STATUS", "show");
				option_status = "show";
			}
			else if(option_status.equalsIgnoreCase("show"))
			{
				iCloud.removeExtra("OPTION_DISPLAY_STATUS");
				iCloud.putExtra("OPTION_DISPLAY_STATUS", "hide");
				
//				Log.d("character","if hide real"+iCloud.getExtras().get("OPTION_DISPLAY_STATUS"));
//				Log.d("character","if hide variable"+option_status);
				option_status = "hide";
			}
			else
				iCloud.removeExtra("OPTION_DISPLAY_STATUS");
//			Log.d("character","-------"+option_status);
//			Log.d("character","------- real"+iCloud.getExtras().get("OPTION_DISPLAY_STATUS"));
			
			PendingIntent pendingCloud = PendingIntent.getService(context,3, iCloud,PendingIntent.FLAG_UPDATE_CURRENT);
			views.setOnClickPendingIntent(R.id.messageTxt,pendingCloud);
			if(option_status.equalsIgnoreCase("hide"))
				views.setViewVisibility(R.id.inputLayout, View.GONE);
			else if(option_status.equalsIgnoreCase("show"))
				views.setViewVisibility(R.id.inputLayout, View.VISIBLE);
			else
				views.setViewVisibility(R.id.inputLayout, View.INVISIBLE);
		

//		Intent intentConfig = new Intent(context, WNWService.class);  
//		
//		
//		PendingIntent pendingIntent = PendingIntent.getService(context, 0, intentConfig, 0);
//          
//		views.setOnClickPendingIntent(R.id.widget, pendingIntent);
//		views.setViewVisibility(R.id.button1, View.GONE);



		
		ComponentName cn = new ComponentName(context,
				WNWProvider.class);
		AppWidgetManager.getInstance(context).updateAppWidget(cn, views);

		return views;
	}
	public static String getCharacterWallpaper(Context context) {
		
		return config_wallpaper_char;
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}


}
